/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.manager;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;
import com.atjava.jcrt.JcrtSession;
import com.atjava.jcrt.JcrtSessionFactory;
import com.atjava.jcrt.core.exception.CoreException;
import com.atjava.jcrt.core.query.ConditionData;
import com.atjava.jcrt.core.query.Query;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.LinkInfo;

/**
 * LinkManager
 * @author biggie
 *
 */
public class LinkManager {
	private static LinkManager manager = null;
	private JcrtSessionFactory factory = null;
	private JcrtSession jsession = null;
	 /**
     * Log4j
     */
    private Logger log = Logger.getLogger(this.getClass().getName());
    
	public static LinkManager getInstance() throws JcrWebException{
		if(manager == null){
			manager = new LinkManager();
		}
		return manager;
	}
	
	private LinkManager() throws JcrWebException{
		try {
			factory = JcrtSessionFactory.getInstance();
			jsession = factory.openJcrtSession();
		} catch (CoreException e) {
			log.error("LinkManager",e);
			throw new JcrWebException(e);
		}
	}
	
     public void createLink(LinkInfo linkinfo) throws  JcrWebException{
		try {
			synchronized(this){
				long id = DataHelper.generatorId(jsession,linkinfo.getClass());
				linkinfo.setId(id);
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(System.currentTimeMillis());
				linkinfo.setDatetime(c);	
				jsession.save(linkinfo, "/"+id);
			}
		} catch (CoreException e) {
			log.error("createLink("+linkinfo+")",e);
			throw new JcrWebException(e);
		} catch (Exception e){
			log.error("createLink("+linkinfo+")",e);
			throw new JcrWebException(e);
		}
	}
     
     public void updateLink(LinkInfo linkinfo) throws  JcrWebException{
 		try {
 			synchronized(this){
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(System.currentTimeMillis());
				linkinfo.setDatetime(c);	
	 			jsession.save(linkinfo, "/"+linkinfo.getId());
 			}
 		} catch (CoreException e) {
 			log.error("updateLink("+linkinfo+")",e);
 			throw new JcrWebException(e);
 		} catch (Exception e){
 			log.error("updateLink("+linkinfo+")",e);
 			throw new JcrWebException(e);
 		}
 	}
     
     public void deleteLink(long id) throws JcrWebException{
     	try{
 			jsession.delete(LinkInfo.class,"/"+id);
 		}catch(CoreException e){
 			log.error("deleteLink("+id+")",e);
 		}catch(Exception e){
 			log.error("deleteLink("+id+")",e);
 			throw new JcrWebException(e);
 		}
     	
     }
     
     public List getLinksByUserCode(String userCode)throws JcrWebException{
 		List results = null;
 		try{
 			ConditionData data = new ConditionData();
 			data.setCondition("createrCode", userCode, ConditionData.CONDITION_EQUAL);
 			Query query = jsession.createQuery(LinkInfo.class, "/", data);
 			results = query.getDefaultResults();
 		}catch(CoreException e){
 			log.error("getLinksByUserCode("+userCode+")",e);
 		}catch(Exception e){
 			log.error("getLinksByUserCode("+userCode+")",e);
 			throw new JcrWebException(e);
 		}
 	   return results;
 	}
     
}
